package com.arelikebrothers.demo.concurrent.jdk;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * Created by lennon on 01/05/2017.
 */
public class CountDownLatchDemo implements Runnable{

    static CountDownLatch end = new CountDownLatch(10);

    @Override
    public void run() {
        try {
            Thread.sleep(new Random().nextInt(10) * 1000L);
            System.out.println("Check complete");
            end.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatchDemo demo = new CountDownLatchDemo();
        ExecutorService exec = Executors.newFixedThreadPool(10);
        IntStream.range(0,10).forEach((i)-> exec.submit(demo));
        end.await();
        exec.shutdown();
    }
}
